<!DOCTYPE html>
<html lang = "pt-BR">
    <head>
        <?php
        session_start();
        include_once'bloco/head.php';
        include_once '../control/conexao.php';
        include_once '../control/valida_sessao.php';
        include_once '../model/empresaDAO.class.php';
        include_once '../model/empresaDAO.class.php';

        function __autoload($classe) {
            include_once"../model/{$classe}.class.php";
        }
        ?>
        <link href="bootstrap/css/morris.css" rel="stylesheet">
    </head>
    <body>      
        <div id="wrapper">
            <?php
            include_once'bloco/navbar.php';
            include_once '../control/valida_perfil.php';
            ?>
            <div id="page-wrapper">
                <?php include_once 'bloco/banner.php'; ?>                  
                <div class="row">
                    <div class=" panel panel-success">
                        <div class="panel-heading">
                            <p class="panel-title"><i class = "glyphicon glyphicon-th-large"></i> Listar Empresa</p>
                        </div>
                        <div class="panel-body">
                            <form autocomplete="off" role="form" name="form_busca_empresa" method="get" action="listar_empresa2.php">
                                <div class="alert alert-success alert-dismissable">
                                    <div class="container-fluid">                                        
                                        <div class="col-md-2 col-md-offset-3">
                                            <select class="btn btn-block" required="" id="parametro" name="parametro">
                                                <option value=""> -- Selecione -- </option>
                                                <option value="cnpj">CNPJ</option>
                                                <option value="sigla">Sigla / Nome Fantasia</option>
                                            </select>
                                        </div>      
                                        <div class="col-md-4">
                                            <div class="input-group">                                                            
                                                <input type="text" class="form-control" id="txt_parametro" name="txt_parametro" maxlength="17" required placeholder="Primeiro selecione o tipo de pesquisa">
                                                <span class="input-group-btn">
                                                    <button class="btn btn-success" type="submit">Buscar</button>
                                                </span>
                                            </div>
                                        </div>                                        
                                    </div>
                                </div>                        
                            </form>                            
                            <div class="col-lg-12">                                
                                <div class="row">
                                    <div class="col-lg-12">                                        
                                        <?php
                                        if (isset($_GET['pg'])) {
                                            $pg = $_GET['pg'];
                                        } else {
                                            $pg = 0;
                                        }
                                        $numreg = 10; // Quantos registros por página vai ser mostrado
                                        $inicial = $pg * $numreg;
                                        $limite = "LIMIT $inicial, $numreg";

                                        $empresa = new empresa();
                                        $empresaDAO = new empresaDAO();

                                        $arEmpresas = $empresaDAO->listar($empresa, $limite);
                                        $quantreg = count($empresaDAO->listar($empresa, NULL));
                                        ?>
                                        <table class="table table-responsive table-striped table-condensed">
                                            <tr>
                                                <td ><strong>Sigla / Nome Fantasia</strong></td>
                                                <td align="center"><strong>CNPJ</strong></td>                                                
                                                <td align="center"><strong>Data de Cadastro</strong></td>                                                
                                            </tr>
                                            <?php
                                            for ($c = 0; count($arEmpresas) > $c; $c++) {
                                                ?>
                                                <tr>
                                                    <td><a href="exibir_empresa.php?id=<?php echo $arEmpresas[$c]->id_empresa ?>"><?php echo utf8_encode($arEmpresas[$c]->empresa_sigla) ?></a></td>
                                                    <td align="center"><?php echo $arEmpresas[$c]->empresa_cnpj ?></td>                                                    
                                                    <td align="center"><?php echo $arEmpresas[$c]->empresa_data ?></td>                                                    
                                                </tr>
                                            <?php } ?>
                                        </table>
                                    </div>
                                </div>                                
                            </div>
                            <div class="col-lg-4 col-lg-offset-4" align="center">
                                <?php
                                include 'bloco/paginacao.php';
                                ?>                                
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <?php
        include_once'bloco/scripts.php';
        ?>
        <script src="bootstrap/js/mascaras.js"></script>
        <script src="bootstrap/js/mask/mask.js"></script>
        <script type="text/javascript">
            $("#parametro").change(function() {
                var opt = $('#parametro option:selected').val();
                if (opt === 'cnpj') {
                    $("#txt_parametro").mask("99.999999/9999-99");
                } else {
                    $("#txt_parametro").unmask();
                }
            });
        </script>
    </body>
</html>